/**
 * 
 */
package com.ydy.controller;

import java.io.File;
import java.util.concurrent.atomic.AtomicInteger;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.ydy.annotation.AdminToken;
import com.ydy.annotation.StaffToken;
import com.ydy.controller.base.BaseController;
import com.ydy.exception.BusinessException;
import com.ydy.ienum.EnumSystem;
import com.ydy.utils.DateUtil;
import com.ydy.utils.FileUtil;

/**
 * @author xuzhaojie
 *
 *         2018年12月17日 上午9:17:29
 */
@Controller
@RequestMapping(value = "file", name = "文件模块")
public class FileController extends BaseController {
	private final static Logger log = LoggerFactory.getLogger(FileController.class);
	private final String[] IMG_FORMAT = { "jpg", "jpeg", "png" };

	private static final AtomicInteger AI = new AtomicInteger(0);

	private static Long getUploadName() {
		synchronized (FileController.class) {
			if (AI.get() >= 9)
				AI.set(0);
			return System.currentTimeMillis() + AI.incrementAndGet();
		}
	}

	@Value("${img_location}")
	private String imgLocation;

	@Value("${img_httpurl}")
	private String imgHttpurl;

	@AdminToken
	@PostMapping(value = "imgUpload", name = "管理员上传图片")
	@ResponseBody
	public ResponseEntity<String> imgUpload(@RequestParam("file") MultipartFile file, HttpServletRequest request)
			throws Exception {
		String fileName = file.getOriginalFilename(); // 图片名字
		if (!FileUtil.isFitFormat(fileName, IMG_FORMAT)) {
			throw new BusinessException(EnumSystem.FILE_TYPE_NOT_FIT);
		}
		// 文件存放路径
		String abPath = imgLocation + File.separator + DateUtil.getTodatDate();
		fileName = getUploadName() + "." + FileUtil.getSuffix(fileName);
		// 调用文件处理类FileUtil，处理文件，将文件写入指定位置
		abPath = FileUtil.uploadFile(file.getBytes(), abPath, fileName);
		// 返回图片的存放路径
		String httpUrl = abPath.replace(imgLocation, imgHttpurl);
		log.info("图片http路径:" + httpUrl);
		return ResponseEntity.ok(httpUrl);
	}

	@StaffToken
	@PostMapping(value = "staffUploadImg", name = "店员上传图片")
	@ResponseBody
	public ResponseEntity<String> staffUploadImg(@RequestParam("file") MultipartFile file, HttpServletRequest request)
			throws Exception {
		String fileName = file.getOriginalFilename(); // 图片名字
		if (!FileUtil.isFitFormat(fileName, IMG_FORMAT)) {
			throw new BusinessException(EnumSystem.FILE_TYPE_NOT_FIT);
		}
		// 文件存放路径
		String abPath = imgLocation + File.separator + DateUtil.getTodatDate();
		fileName = getUploadName() + "." + FileUtil.getSuffix(fileName);
		// 调用文件处理类FileUtil，处理文件，将文件写入指定位置
		abPath = FileUtil.uploadFile(file.getBytes(), abPath, fileName);
		// 返回图片的存放路径
		String httpUrl = abPath.replace(imgLocation, imgHttpurl);
		log.info("图片http路径:" + httpUrl);
		return ResponseEntity.ok(httpUrl);
	}

}